package com.hot100;

public class Solution581 {
    public int findUnsortedSubarray(int[] nums) {
        int left = 1;
        int right = 0;
        int max = nums[0];
        int min = nums[nums.length - 1];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] >= max) max = nums[i];
            else right = i;
            if (nums[nums.length - 1 - i] <= min) min = nums[nums.length - 1 - i];
            else left = nums.length - i - 1;
        }
        return right - left + 1;
    }
}
